package com.app.ytong.user;

import android.os.Bundle;
import android.widget.EditText;

import com.app.ytong.basis.BasisActivity;
import com.app.ytong.R;
import com.app.ytong.basis.Constants;
import com.app.ytong.bean.BasisBean;

import butterknife.BindView;
import butterknife.OnClick;
import my.DialogUtils;
import my.MySharedPreferences;
import my.http.MyHttpUtils;
import my.http.MyHttpListener;
import my.http.MyParams;

public class ModifyPwdActivity extends BasisActivity {


    @BindView(R.id.edit_new_pwd)
    EditText edit_new_pwd;
    @BindView(R.id.edit_old_pwd)
    EditText edit_old_pwd;
    @BindView(R.id.edit_pwd_confirm)
    EditText edit_pwd_confirm;

    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.user_modify_pwd_activity);

        setTitleLeftButton(null);
        setTitle(R.string.user_modify_password);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    @OnClick(R.id.btn_ok)
    void post() {
        String pass = edit_old_pwd.getText().toString();
        String passNew = edit_new_pwd.getText().toString();
        String passAgain = edit_pwd_confirm.getText().toString();


        if (pass.length() == 0) {
            showToast("请输入旧密码");
            return;
        }

        if (passNew.length() == 0) {
            showToast("请输入新密码");
            return;
        }

        if (passAgain.length() == 0) {
            showToast("请再次输入新密码");
            return;
        }

        if (passNew.length() < 6) {
            showToast("密码最少6位");
            return;
        }

        if (!passNew.equals(passAgain)) {
            showToast("确认密码和新密码不一致，请重新输入");
            edit_new_pwd.setText("");
            edit_pwd_confirm.setText("");
            return;
        }

        if (passNew.equals(pass)) {
            showToast("新密码不能与原密码相同");
            return;
        }

        MyParams params = new MyParams();
        MySharedPreferences mSp = new MySharedPreferences(this);

//        params.put("oldPassword", MD5.getMD5ofStrLowercase(pass));
//        params.put("newPassword",  MD5.getMD5ofStrLowercase(passNew));
        params.put("oldPassword",pass);
        params.put("newPassword",  passNew);
        showLoading();
        MyHttpUtils.post(Constants.URL_USER_MODIFY_PWD, params, new MyHttpListener() {
            @Override
            public void onSuccess(int httpWhat, Object result) {
                mSp.putPassword(passNew);
                DialogUtils.DialogOKmsgFinish(mContext,((BasisBean)result).getResultData());
            }

            @Override
            public void onFinish(int httpWhat) {
                hideLoading();
            }
        }, 0, BasisBean.class);

    }


}
